package main

import (
	"fmt"
	"go-study/src/sjcc/consts"
	"go-study/src/sjcc/db"
	"go-study/src/sjcc/models"
	"go-study/src/sjcc/utils"
	"golang.org/x/net/html"
	"log"
	"net/http"
	"strconv"
	"strings"
	"time"
)

// 研招网院校库
const URL = "https://yz.chsi.com.cn/sch/?start="

// 学校名称
const SchoolNameTagName = "a"

const SchoolNameTagKey = "class"

const SchoolNameTagValue = "name js-yxk-yxmc text-decoration-none"

// 学校id
const SchoolIdTagKey = "href"

// 院校隶属
const BelongTagName = "span"

const BelongTagKey = "class"

const BelongTagValue = "item-depart-title"

// 院校地址
const LocaltionTagName = "div"

const LocaltionTagKey = "class"

const LocaltionTagValue = "sch-department"

// 考试类型
const TypeTagName = "i"

const TypeTagKey = "class"

const TypeTagValue = "sch-department"

// 解析html
func GetSchoolArray() ([]models.School, []models.Department) {

	var currentPage int
	var schoolLists []models.School
	var departmentLists []models.Department
	var count int = 1

	for currentPage = 0; currentPage <= 860; currentPage += 20 {
		fmt.Println(time.Now().String() + "========= 当前爬取第 " + strconv.Itoa(count) + "页 =======")
		resp, err := http.Get(URL + strconv.Itoa(currentPage))
		if err != nil {
			log.Fatal(err)
		}
		defer resp.Body.Close()

		doc, err := html.Parse(resp.Body)
		if err != nil {
			log.Fatal(err)
		}
		schoolLists, departmentLists = QuerySchoolTag(doc, schoolLists, departmentLists)

		count++
	}

	return schoolLists, departmentLists
}

// 获取院系信息和专业信息
func GetMajor(department models.Department) models.Department {
	requestUrl := "https://yz.chsi.com.cn/sch/listYzZyjs--schId-367878,categoryId-692157514.dhtml"
	resp, err := http.Get(requestUrl)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()
	doc, err := html.Parse(resp.Body)
	if err != nil {
		log.Fatal(err)
	}
	print(doc)
	return department
}

// 解析标签
func QuerySchoolTag(n *html.Node, schoolArray []models.School, departmentArray []models.Department) ([]models.School, []models.Department) {

	// 定义一个存储院校的切片
	//schoolSlice = make([]string, 0, 200) // 初始容量为200
	if schoolArray == nil {
		schoolArray = make([]models.School, 0)
	}

	// 定义一个存储院校专业库类目的切片
	if departmentArray == nil {
		departmentArray = make([]models.Department, 0)
	}

	// 院校名称标签抓取
	if n.Type == html.ElementNode &&
		n.Data == SchoolNameTagName &&
		utils.CheckAttributes(n, "class", "href") {

		for _, attr := range n.Attr {

			// 得到院校名称
			if attr.Key == SchoolNameTagKey &&
				attr.Val == SchoolNameTagValue {

				// 得到院校id  href标签
				var schoolId int64
				if n.Attr[1].Key == SchoolIdTagKey &&
					strings.Contains(n.Attr[1].Val, "/sch/schoolInfo--schId-") {
					schId, err := utils.GetSchId(n.Attr[1].Val)
					if err == nil {
						schoolId = schId
					} else {
						schoolId = 0
					}
				}

				// 得到院校名称
				data := n.FirstChild.Data
				school := models.School{
					Id:          schoolId,
					SchoolName:  data,
					Belong:      "归属",
					Location:    "院校地址",
					Type:        0,
					UtcCreate:   time.Now(),
					UtcModified: time.Now(),
				}

				// 获取院校专业类目id
				categoryId := utils.GetRequestCategoryIdValue(schoolId)
				fmt.Println("schoolId: " + strconv.Itoa(int(schoolId)) + " " + "categoryId: " + strconv.Itoa(int(categoryId)))
				log.Println("data: " + school.SchoolName)

				department := models.Department{
					ID:             categoryId,
					UTCCreate:      time.Now(),
					UTCModified:    time.Now(),
					SchoolID:       schoolId,
					SchoolName:     school.SchoolName,
					DepartmentName: "",
				}

				log.Println("schoolName: " + school.SchoolName + "     department: " + department.SchoolName)

				// 拿department的院系id和类目id 请求获取研招网院校信息类目下的院系信息和专业信息
				//department = GetMajor(department)

				departmentArray = append(departmentArray, department)
				schoolArray = append(schoolArray, school)
			}
		}
	}

	// 遍历子节点
	for c := n.FirstChild; c != nil; c = c.NextSibling {
		schoolArray, departmentArray = QuerySchoolTag(c, schoolArray, departmentArray)
	}

	return schoolArray, departmentArray
}

func main() {
	fmt.Println("school program is starting-------->")
	schoolArray, departmentArray := GetSchoolArray()
	//log.Print(schoolArray)
	fmt.Println("school program ending-------->")
	fmt.Println("school nums：" + strconv.Itoa(len(schoolArray)))
	fmt.Println("department nums：" + strconv.Itoa(len(departmentArray)))
	fmt.Println("starting save school data----->")
	gormDB1 := consts.GetGormDB()
	gormDB2 := consts.GetGormDB()
	db.BatchInsertSchool(gormDB1, schoolArray)
	db.BatchInsertDepartment(gormDB2, departmentArray)

	//GetMajor(models.Department{})
}
